//
//  WXPBaseAuthVC.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/28.
//

#import "WXPBaseAuthVC.h"

@interface WXPBaseAuthVC ()

@end

@implementation WXPBaseAuthVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (_navView == nil) {
        _navView = [WXPNavView  new];
        [_navView resetViewWithTitle:@""];
        _navView.leftTop = XY(0, 0);
        WEAKSELF
        _navView.blockPop = ^{
            [weakSelf.view endEditing:true];
            if(weakSelf.immediatePop){
                [Global_Nav popToRootViewControllerAnimated:true];
            }else{
                // 显示挽留
                weakSelf.immediatePop = true;
                [weakSelf.view addSubview:weakSelf.detainmentView];
            }
        };
    }
    return  _navView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [UIView new];
    }
    return _headerView;
}
- (WXPAuthProgressView *)progressView{
    if (!_progressView) {
        _progressView = [WXPAuthProgressView new];
        [_progressView resetViewWithNum:@"0"];
        [_progressView.moneyView resetViewWithTag:0];
    }
    return _progressView;
}
- (UIView *)btnAuthView{
    if (!_btnAuthView) {
        _btnAuthView = [UIView new];
        _btnAuthView.widthHeight = XY(SCREEN_WIDTH, BUTTON_HEIGHT);
        _btnAuthView.backgroundColor = [UIColor clearColor];
        _btnAuthView.userInteractionEnabled = true;
        self.btnAuth.centerXTop = XY(SCREEN_WIDTH/2, 0);
        [_btnAuthView addSubview:self.btnAuth];
    }
    return _btnAuthView;
}
- (UIButton *)btnAuth{
    if (!_btnAuth) {
        _btnAuth = [UIButton new];
        _btnAuth.widthHeight = XY(SCREEN_WIDTH-2*LEFT_MARDIN, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnAuth color:[UIColor clearColor] numRound:_btnAuth.height/2 width:0];
        _btnAuth.backgroundColor = COLOR_PRIMARY;
        _btnAuth.alpha = 0.5;
        [_btnAuth setTitle:@"Entregar" forState: UIControlStateNormal];
        _btnAuth.titleLabel.fontNum = 14;
        [_btnAuth setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnAuth.userInteractionEnabled = false;
    }
    return _btnAuth;
}
- (WXPCommonAgreementView *)agreementView{
    if (!_agreementView) {
        _agreementView = [WXPCommonAgreementView new];
        _agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-LEFT_MARDIN);
    }
    return _agreementView;
}
- (WXPPopDetainmentView *)detainmentView
{
    if (_detainmentView == nil) {
        _detainmentView = [WXPPopDetainmentView  new];
    }
    return  _detainmentView;
}

#pragma mark - viewDidLoad
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.startTime = [GlobalMethod getTimeStampMillisecond];
    self.navigationController.interactivePopGestureRecognizer.enabled = false;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [self.view addGestureRecognizer:tapGestureRecognizer];
    [self addObserveOfKeyboard];
    self.view.backgroundColor = COLOR_BACKGROUND;
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navView];
    //[self.view addSubview:self.agreementView];
    self.btnAuthView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-MAX(15, [GlobalMethod getBottomSafeAreaHeight]-10));
    [self.view addSubview:self.btnAuthView];
    self.tableView.height = self.btnAuthView.top-[GlobalMethod getNavgationBarHeight]-10;
    [self requestAwardAmount];
}

#pragma mark - 键盘
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{
    [self.view endEditing:YES];
}
- (void)tapClick:(UITapGestureRecognizer * )tap{
    [self.view endEditing:true];
}

#pragma mark - request
// 所有步骤的奖励金钱
- (void)requestAwardAmount{
    [RequestApi requestAwardAmountWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        [self.aryDatas addObject:[response stringValueForKey:KEY_CREDIT_INFO]];
        [self.aryDatas addObject:[response stringValueForKey:KEY_IDENTITY_INFO]];
        [self.aryDatas addObject:[response stringValueForKey:KEY_LIVENESS_INFO]];
        [self.aryDatas addObject:[response stringValueForKey:KEY_PERSONAL_INFO]];
        [self.aryDatas addObject:[response stringValueForKey:KEY_INCOME_INFO]];
        [self.aryDatas addObject:[response stringValueForKey:KEY_CONTACT_INFO]];
        [self.aryDatas addObject:[response stringValueForKey:KEY_BANK_INFO]];
        if(self.aryDatas.count >= 7){
            [self.progressView.moneyView resetViewWithMoney:self.aryDatas];
        }
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

// 下一步
- (void)requestGetNextStep{
    [RequestApi requestGetNextStepNewWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        NSString * step = @"";
        if (response && [response isKindOfClass:[NSDictionary class]] && response.count > 0) {
            for (NSString * key in response) {
                id value = response[key];
                if([value isKindOfClass:[NSString class]]){
                    if ([value isEqualToString:@"1"]) {
                        step = key;
                        break;
                    }
                }
            }
        }else{
            // 认证完成
            step = @"AUTH_SUCCESS";
        }
        
        [GlobalMethod jumpToAuthWithAuthInfo:step];
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

@end
